<?php
//<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
class Gallery extends Controller
{
	function setVars()
	{
        $this->path = BASEPATH.'../img/gallery/';
        define('URI',$this->uri->segment(1).'/'.$this->uri->segment(2));
        //define('GETURL',site_url(URI).'/');
        //define('GETURL','http://localhost/index.php/'GETURL.URI);
	}
    
	function __construct()
	{
		parent::Controller();
		$this->setVars();
        //$this->load->library('pquery');
        //$this->load->library('profiler');
        log_message('debug', "Gallery: Controler Class Initialized");
	}

    /**
    * @note	index
    * @info	Just forwarding
    * @author	Dennis T. Kaplan
    * @access	public
    * @param	 
    * @return	html page
    */	
    public function index()
    {
		$this->tpl['title'] = "Gallery";
        $this->load->model('Tamer_Model', 'tm');
        //$this->tpl['sidebar'] = $this->sidebar();
		$this->tpl['head'] = '';
        $this->tpl['body'] = '';
        $this->tpl['sidebar'] = $this->tm->randomBlock();
        $this->tpl['body'] .= '
<div><object type="application/x-shockwave-flash" allowScriptAccess="never" allowNetworking="internal" height="319" width="426" data="http://apps.rockyou.com/rockyou.swf?instanceid=93561075&ver=102906">
  <param name="allowScriptAccess" value="never" />
  <param name="allowNetworking" value="internal" />
  <param name="movie" value="http://apps.rockyou.com/rockyou.swf?instanceid=93561075&ver=102906" />
  <param name="quality" value="high" />
  <param name="salign" value="lt" />
  <param name="wmode" value="transparent" />
</object></div>';
        $this->load->view('blank', $this->tpl);

    }
    /**
    * @note	index
    * @info	Just forwarding
    * @author	Dennis T. Kaplan
    * @access	public
    * @param	 
    * @return	html page
    */	
    public function indexOLD()
    {
        $this->load->model('Tamer_Model', 'tm');
        $this->uid = $this->tm->get_uid();
        
        log_message('info', "Gallery: INDEX");
		$this->tpl['title'] = "Gallery";
        //$this->tpl['sidebar'] = $this->sidebar();
		$this->tpl['head'] = '';
        $this->tpl['body'] = '';
        
        $this->tpl['tpljs'][]  = 'jquery';
        $this->tpl['tpljs'][]  = 'jquery.innerfade';

        $this->load->library('image_lib');
        $this->load->helper('directory');
        //$this->load->helper('img');
        
        $result=0; // Counter
        if(isset($this->uid) && $this->uid == 1){
            $this->msg->setMsg('Updating image directory!');
            $gallery = directory_map($this->path);
            $org=$gallery['org'];
        }else{
            $org = directory_map($this->path.'small/');
        }
        foreach($org as $k => $v){
            if ($v != 'index.html'){
                $map[] = $v;
                $result++;
            }
        }
        if(isset($this->uid) && $this->uid == 1){
            foreach($map as $k => $v){
                if ( ! in_array($v, $gallery['small'])) {
                    $this->resizeSmall($v);
                }
                if ( ! in_array($v, $gallery['mid'])) {
                    $this->resizeMid($v);
                }
            }
        }

        $i=0;
        while($i<$result){
            $j=0;
            $tickerimgtmp='';
            while( ($j<4) && ($i<$result) ){
                $v = $map[$i];
                $tickerimgtmp .= $this->ticker($v);
                $i++;
                $j++;
            }
            $tickerimg[] = $tickerimgtmp;
        }
        
        $this->tpl['head'] = "
		<script type=\"text/javascript\">
        $(document).ready(
        function(){
            $('#news').innerfade({
                animationtype: 'slide',
                speed: 3000,
                timeout: 20000,
                type: 'sequence',
                containerheight: '16em'
            });
            
            $('ul#portfolio').innerfade({
                animationtype: 'slide',
                speed: '5000',
                timeout: 5000,
                type: 'sequence',
                containerheight: '100px'
            });
        });
        </script>";
        
        
        $i = '<ul id="portfolio" style="padding: 0px; ">';
        foreach($tickerimg as $v){
            $i .= '<li>'.$v.'</li>';
        }
        $i .= '</ul>';
        

        $this->tpl['sidebar'] = '<h4 class="SideBar">Welcome to the PzzAzz image Gallery. Click on the thumbnails to see the fullsize image</h4>';
        $this->tpl['sidebar'] .= $this->tm->randomBlock();
        $this->tpl['body'] .= '<img src="'.BASEURL.'img/blank.gif" width="500" height="1" alt="">';
        $this->tpl['body'] .= $i;
        $this->tpl['body'] .= '<div id="image"><img src="'.BASEURL.'img/gallery/mid/'.$map[2].'" alt="Mobile Advertising Truck"></div>';
        $this->load->view('blank', $this->tpl);
    }


    private function ticker($v)
    {
        $imgurl = '<img src="'.BASEURL.'img/gallery/small/'.$v.'" alt="Mobile Advertising Truck">';
        return $this->pquery->link_to_remote($imgurl,array('url'=>GETURL.URI."ajax/$v",'update'=>'#image'));;
    }

    /**
     * @note	sidebar
     * @info	Create sidebar content
     * @author	Dennis T. Kaplan
     * @access	private
     * @param	 
     * @return	String
     */	
    private function blocks()
    {
        $sql = "SELECT `Title`, `Body` FROM `blocks` ORDER BY RAND()";
        $query = $this->db->query($sql);
        $row = $query->result();
        //printa($row);
        $i = '<ul id="news" style="padding: 2px; ">';
        foreach($row as $v){
            $i .= "<li><h3>$v->Title</h3>$v->Body</li>";
        }
        $i .= '</ul>';
        return $i;
    }
    
	public function ajax()
	{
        log_message('debug', "Gallery: Ajax call");
        echo '<img src="'.BASEURL.'img/gallery/mid/'.$this->uri->segment(3).'" alt="Mobile Advertising Truck">';
	}    
    
    
    /**
    * @note	resizeSmall
    * @info	Resize an image to small size
    * @author	Dennis T. Kaplan
    * @access	private
    * @param	$size 
    * @return	
    */	
    private function resizeSmall($img)
    {
        $config['image_library'] = 'ImageMagick'; //'GD';
        $config['library_path'] = '/usr/bin/';
        $config['source_image'] = $this->path.'org/'.$img;
        $config['new_image'] = $this->path.'small/'.$img;
        $config['create_thumb'] = TRUE;
        $config['maintain_ratio'] = TRUE;
        $config['master_dim'] = 'hight';
        $config['thumb_marker'] = '';
        $config['quality'] = '100%';
        $config['width'] = 100;
        $config['height'] = 100;
        $this->image_lib->initialize($config); 
        
        $this->image_lib->resize();
    }
    
    
    /**
    * @note	resizeMid
    * @info	Resize an image to mid size
    * @author	Dennis T. Kaplan
    * @access	private
    * @param	$size 
    * @return	
    */
    
    private function resizeMid($img)
    {
        $config['image_library'] = 'ImageMagick'; //'GD';
        $config['library_path'] = '/usr/bin/';
        $config['source_image'] = $this->path.'org/'.$img;
        $config['new_image'] = $this->path.'mid/'.$img;
        $config['create_thumb'] = TRUE;
        $config['maintain_ratio'] = TRUE;
        $config['master_dim'] = 'width';
        $config['thumb_marker'] = '';
        $config['width'] = 400;
        $config['height'] = 400;
        $this->image_lib->initialize($config); 
        
        $this->image_lib->resize();
    }
}
?>
